/**
 * Project Name:jbase
 * File Name:DefaultImgFindMemoryServiceProvider.java
 * Package Name:jbase.project.system.net.http.websearch
 * Date:2017年4月30日下午6:33:08
 * Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
 *
*/

package jbase.project.system.net.http.websearcher;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jbase.project.system.core.common.Console;
import jbase.project.system.core.common.EventHook;
import jbase.project.system.util.FileUtils;

/**
 * ClassName:DefaultImgFindMemoryServiceProvider <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2017年4月30日 下午6:33:08 <br/>
 * 
 * @author Licheng
 * @version
 * @since JDK 1.8
 * @see
 */
public class DefaultImgFindMemoryServiceProvider implements TaskServiceProvider, Console {

	private LinkedList<String> tasks = new LinkedList<>();
	private LinkedList<String> errors = new LinkedList<>();
	private LinkedList<String> finished = new LinkedList<>();

	private LinkedList<String> allowRegex = new LinkedList<>();
	private LinkedList<String> refuseRegex = new LinkedList<>();
	private LinkedList<String> downRegex = new LinkedList<>();

	private EventHook hook;
	private String output = null;
	public volatile int index = 0;

	public DefaultImgFindMemoryServiceProvider(String outputDirectory, EventHook hook, Collection<String> starts,
			Collection<String> allowRegex, Collection<String> refuseRegex, Collection<String> downRegex) {
		this.output = outputDirectory;
		this.hook = hook;
		this.tasks.addAll(starts);
		this.allowRegex.addAll(allowRegex);
		this.refuseRegex.addAll(refuseRegex);
		this.downRegex.addAll(downRegex);
	}

	private boolean isDownSource(String href) {
		boolean pass = false;
		for (String reg : downRegex) {
			if (href.matches(reg)) {
				pass = true;
			}
		}
		return pass;
	}

	@Override
	public void addURLToTasks(String fullURL) {
		tasks.addLast(fullURL);
	}

	@Override
	public void finishURL(String url) {
		this.finished.addLast(url);
	}

	@Override
	public void errorURL(String url) {
		errors.addLast(url);
	}

	@Override
	public String getNextURL() {
		if (tasks.size() > 0) {
			return tasks.removeFirst();
		} else {
			return null;
		}
	}

	public boolean isExists(String url) {
		return tasks.contains(url) || errors.contains(url) || finished.contains(url);
	}

	@Override
	public <T, R> R executeCommand(String command, T... objects) {
		return (R) (Object) true;
	}

	@Override
	public boolean save(String url, byte[] content) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(this.output + "\\" + index++ + ".jpg");
			os.write(content);
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				os.close();
			} catch (IOException e) {
			}
		}
		return true;
	}

	@Override
	public boolean isURLInTaskRange(String url) {
		return isPassTaskChecked(url);
	}

	private boolean isPassTaskChecked(String src) {
		boolean pass = true;
		int size = 0;
		Pattern p = null;
		Matcher m = null;

		size = allowRegex.size();
		for (int i = 0; i < size; i++) {
			p = Pattern.compile(allowRegex.get(i));
			m = p.matcher(src);
			if (!m.matches()) {
				pass = false;
				break;
			}
		}
		if (pass) {
			size = refuseRegex.size();
			for (int i = 0; i < size; i++) {
				p = Pattern.compile(refuseRegex.get(i));
				m = p.matcher(src);
				if (m.matches()) {
					pass = false;
					break;
				}
			}
		}
		return pass;
	}

	@Override
	public boolean isDownLoadSource(String url) {
		return isDownSource(url);
	}
}
